package com.libs.sys.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.libs.sys.model.Seat;
import com.libs.sys.service.SeatService;
import com.libs.utils.HttpClientConn;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB 前端控制器
 * </p>
 *
 * @author SR.Y
 * @since 2021-08-11
 */
@Slf4j
@RestController
@RequestMapping("/seat")
public class SeatController {

    @Autowired
    private SeatService seatService;
    final public int LABID1 = 100455370;
    final public int LABID7 = 100455374;


    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Seat getOne(@PathVariable String id) {
        log.info("{}获取签到链接", id);
        Seat seat = seatService.selectoneseat(id);
        return seat;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public void add() throws Exception {
        String url = "https://lib-seat.chu.edu.cn/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id=100456481&purpose=&selectOpenAty=&cld_name=default&date=2021-09-06&fr_start=13%3A18&fr_end=19%3A18&act=get_rsv_sta&_=1630900401154";
        CloseableHttpResponse getRes = HttpClientConn.getGetRes(url, true, HttpClientConn.getHttpClient());
        String body = EntityUtils.toString(getRes.getEntity());
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONArray data = jsonObject.getJSONArray("data");
        for (int i = 0; i < data.size(); i++) {
            JSONObject d = JSONObject.parseObject(data.get(i).toString());
//            System.out.println(d.getString("devId"));
            Seat seat = new Seat(d.getString("devId"), 3);
            System.out.println(seat.getSeatid() + seat.getLabid() + seat.getId());
            seatService.add(seat);
        }
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public List<String> getId(String id, String labid) {
        int Labid = Integer.parseInt(labid);
        int ID = -1;
        List<String> list = new ArrayList<>();
        try {
            ID = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            list.add("-1");
            log.info("【签到】格式有误{}", id);
            return list;
        }
        if (ID <= 0 || ID > 1300) {
            list.add("-1");
            log.info("【签到】格式有误{}", id);
            return list;
        }
        if (Labid == 0) {
            Labid = LABID1;
        } else {
            if (Labid == 1) {
                ID += 462;
            } else if (Labid == 2) {
                ID += 810;
            } else if (Labid == 3) {
                ID += 1080;
            }
            Labid = LABID7;
        }
        Seat seat = seatService.selectoneseat(String.valueOf(ID));
        list.add(String.valueOf(Labid));
        list.add(seat.getSeatid());
        return list;
    }

}

